package com.dailyblue.java.project.power.config;

import com.dailyblue.java.project.power.interceptor.HavePermissionInterceptor;
import com.dailyblue.java.project.power.interceptor.HaveRoleInterceptor;
import com.dailyblue.java.project.power.interceptor.IsLoginInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
@EnableWebMvc
@ComponentScan({"com.dailyblue.java.project.power.controller", "com.dailyblue.java.project.power.interceptor"})
public class SpringMVCConfig implements WebMvcConfigurer {
    @Resource
    private IsLoginInterceptor isLoginInterceptor;
    @Resource
    private HavePermissionInterceptor havePermissionInterceptor;
    @Resource
    private HaveRoleInterceptor haveRoleInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(isLoginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/users/login", "/users/unique", "/users/save", "/powers/execute").order(1);

        registry.addInterceptor(haveRoleInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/users/login", "/users/a", "/powers/execute").order(2);


        registry.addInterceptor(havePermissionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/users/login", "/users/a", "/powers/execute").order(3);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}
